<?php  
  
namespace App\Http\Middleware;  
  
use Closure;  
use JWTAuth;  
use Tymon\JWTAuth\Exceptions\JWTException;  
use Tymon\JWTAuth\Exceptions\TokenExpiredException;  
use Tymon\JWTAuth\Exceptions\TokenInvalidException;  
  use Illuminate\Http\Request;
class CheckToken  
{  
    /**  
     * @param  \Illuminate\Http\Request  $request  
     * @param  \Closure  $next  
     * @return mixed  
     */  
public function handle($request, Closure $next)  
{  
    try {  
  
        if (! $user = JWTAuth::parseToken()->authenticate()) {  
            return response()->json([  
                'errcode' => 1006,  //未获取到用户数据
                'errmsg' => 'token未获取到信息'  
            ], 404);  
        }else{
            $mid_params = JWTAuth::parseToken()->authenticate();
            //dump($a);
            $mid_params = json_decode(json_encode($mid_params),true);
            // dump($mid_params);
            $request->attributes->add($mid_params);//添加参数
            return $next($request);//进行下一步(即传递给控制器)
        }
  
    } catch (TokenExpiredException $e) {  
  
        return response()->json([  
            'errcode' => 1009,  
            'errmsg' => 'token过期' , //token已过期
        ]);  
  
    } catch (TokenInvalidException $e) {  
  
        return response()->json([  
            'errcode' => 1008,  
            'errmsg' => 'token无效',  //token无效
        ]);  
  
    } catch (JWTException $e) {  
  
        return response()->json([ 
            'errcode' => 1007,  
            'errmsg' => 'token为空' , //token为空
        ]);  
  
    }   
}  
}  